package com.edgclub.edg.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.edgclub.edg.R;
import com.edgclub.edg.api.RechargeApi;
import com.edgclub.edg.api.ResultCode;
import com.edgclub.edg.api.param.RechargeParam;
import com.edgclub.edg.http.VolleyListener;
import com.edgclub.edg.uc.DeleteDialog;
import com.edgclub.edg.utils.util.JSONHelper;
import com.edgclub.edg.utils.util.StringUtils;
import com.jungly.gridpasswordview.GridPasswordView;

/**
 * 
 * */
public class GZSetPayPassActivity extends GzBaseActivity {
	private ImageView close;
	private TextView tv_money,tv_wallet;
	private GridPasswordView pswView;
	private RelativeLayout ll_wallet;
	private String totalMoney="";
	private String balance="";
	private boolean isLoading = false; // 是否正在加载
	private RechargeApi rechargeApi;
	private String oldpass="";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_pay_pass_dialog);
		rechargeApi = new RechargeApi(this);
		totalMoney=getIntent().getStringExtra("totalMoney");
		balance=getIntent().getStringExtra("balance");
		initView();
	}

	private  void  initView (){
		ll_wallet = (RelativeLayout) findViewById(R.id.ll_wallet);
		close = (ImageView) findViewById(R.id.close);
		tv_money = (TextView) findViewById(R.id.tv_money);
		tv_wallet = (TextView) findViewById(R.id.tv_wallet);
		pswView = (GridPasswordView) findViewById(R.id.pswView);
		pswView.setOnPasswordChangedListener(listener);
		tv_money.setText(totalMoney);
		tv_wallet.setText(balance);
		close.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				finish();
				closeKeyboard();
				overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
			}
		});
	}
	GridPasswordView.OnPasswordChangedListener listener = new GridPasswordView.OnPasswordChangedListener() {
		@Override
		public void onTextChanged(String psw) {

		}

		@Override
		public void onInputFinish(String psw) {
			oldpass=psw;
			checkPassword();
		}
	};

	private void checkPassword() {
		if (isLoading) {
			return;
		}
		if("".equals(oldpass)){
			Toast.makeText(this,"请输入支付密码",Toast.LENGTH_SHORT).show();
			return;
		}
		RechargeParam atMeParam = new RechargeParam();
		atMeParam.setPwd(oldpass);
		rechargeApi.CheckPass(atMeParam, new getCheckListener());
	}
	class getCheckListener implements VolleyListener {

		@Override
		public void onStart(String tag) {
			isLoading = true;
				showDialog(StringUtils.getString(R.string.getting), true);
		}

		@Override
		public void onSuccess(String response,String tag) {
			isLoading = false;
			dismissDialog();
			if (JSONHelper.isSuccess(response)) {
//				Intent intent=new Intent(GZSetPayPassActivity.this,GzRedPacketSendActivity.class);
//				intent.putExtra("payPwd", oldpass);
//				setResult(GzRedPacketSendActivity.REQUEST_CODE_PAYPassSUCCESS, intent);
				finish();
				closeKeyboard();
				overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
			}else if(9120==JSONHelper.getResultCode(response)){
				banDingDialog1();
			}else if(9121==JSONHelper.getResultCode(response)){
				banDingDialog();
			}else{
				Toast.makeText(GZSetPayPassActivity.this,"支付失败",Toast.LENGTH_SHORT).show();
			}
		}

		@Override
		public void onFaile(VolleyError volleyError,String tag) {
			isLoading = false;
			dismissDialog();
			oldpass="";
			Toast.makeText(GZSetPayPassActivity.this,"支付失败",Toast.LENGTH_SHORT).show();
//			banDingDialog1();
			ResultCode.toastVolleyError(GZSetPayPassActivity.this, volleyError);
		}

	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
//			Intent intent=new Intent(GZSetPayPassActivity.this,GzRedPacketSendActivity.class);
//			setResult(GzRedPacketSendActivity.REQUEST_CODE_PAYSelectSUCCESS,intent);
			finish();
			closeKeyboard();
			overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
			return true;
		}
		return true;
	}
	private void banDingDialog() {
		final DeleteDialog deleteDialog = DeleteDialog.create(this, true);
		deleteDialog.show();
		deleteDialog.setCancelable(true);
		deleteDialog.setTitle(StringUtils.getString(R.string.prompt));
		deleteDialog.setContent("您输入的支付密码错误");
		deleteDialog.setSubmittext("忘记密码");
		deleteDialog.setCancle("重试");
		deleteDialog.setCancelListener(new DeleteDialog.CancelClickListener() {

			@Override
			public void cancelClick() {
				deleteDialog.dismiss();
					pswView.clearPassword();
			}
		});
		deleteDialog.setSubmitListener(new DeleteDialog.SubmitClickListener() {

			@Override
			public void submitClick() {
				deleteDialog.dismiss();
				Intent intent = new Intent(GZSetPayPassActivity.this,
						GzPayPassForgetActivity.class);
				intent.putExtra("frompage", "forget");
				startActivity(intent);
				finish();

			}
		});
	}
	private void banDingDialog1() {
		final DeleteDialog deleteDialog = DeleteDialog.create(this, true);
		deleteDialog.show();
		deleteDialog.setCancelable(true);
		deleteDialog.setTitle(StringUtils.getString(R.string.prompt));
		deleteDialog.setContent("您输入的支付密码超过5次");
		deleteDialog.setSubmittext("重置密码");
		deleteDialog.setCancle("取消");
		deleteDialog.setCancelListener(new DeleteDialog.CancelClickListener() {

			@Override
			public void cancelClick() {
				deleteDialog.dismiss();
				pswView.clearPassword();
				oldpass = "";
			}
		});
		deleteDialog.setSubmitListener(new DeleteDialog.SubmitClickListener() {

			@Override
			public void submitClick() {
				deleteDialog.dismiss();
				Intent intent = new Intent(GZSetPayPassActivity.this,
						GzPayPassForgetActivity.class);
				intent.putExtra("frompage", "forget");
				startActivity(intent);
				finish();
				closeKeyboard();
			}
		});
	}
	private void closeKeyboard() {
		InputMethodManager inputmanger = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		inputmanger.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(),
				InputMethodManager.HIDE_NOT_ALWAYS);
	}
}
